<?php
session_start();

//    var_dump($uid);
//    die;
//    var_dump($_POST);
//    var_dump($id);
switch ($_GET['a']) {
    case 'post':
        $id = $_GET['id'];
        $title = $_POST['title'];
        $content = $_POST['content'];

        $uid = $_SESSION['id'];
        $time = time();
        $link = mysqli_connect("localhost", "root", "") or die("数据连接失败");
        mysqli_set_charset($link, "utf8");
        mysqli_select_db($link, "bbs");
        if (!empty($title) && !empty($content)) {
            $sql = "insert into post (uid,tid,title,content,ctime) values({$uid},{$id},'{$title}','{$content}',{$time})";
            $result = mysqli_query($link, $sql);
            if ($result && mysqli_affected_rows($link) > 0) {
                echo "<script>alert('发帖成功！');window.location.href='../list.php?id={$id}';</script>";

                die;
            } else {
                echo "<script>alert('发帖失败！');window.location.href='../list.php?id={$id}';</script>";
                die;
            }
        }
        break;
    case 'reply':
        $pid = $_GET['pid'];
        $id = $_GET['id'];
        $content = $_GET['content'];
        $content1 = $_POST['content1'];
        $username = $_GET['username'];
        $title = $_GET['title'];
        $time = time();

//            var_dump($pid);
//            die;
        $link = mysqli_connect("localhost", "root", "") or die("数据连接失败");
        mysqli_set_charset($link, "utf8");
        mysqli_select_db($link, "bbs");
        if (!empty($content)) {
            $sql = "insert into reply (uid,pid,content,ctime) values({$_SESSION['id']},{$pid},'{$content1}',{$time})";
            $result = mysqli_query($link, $sql);
            if ($result && mysqli_affected_rows($link) > 0) {
                echo "<script>alert('发帖成功！');window.location.href='../details.php?id={$id}&username={$username}&pid={$pid}&title={$title}&content={$content}';</script>";

                die;
            } else {
                echo "<script>alert('发帖失败！');window.location.href='../list.php?id={$id}';</script>";
                die;
            }
        }
}
